package android.activities;

import java.util.List;

import android.activities.R;
import android.controllers.CourseController;
import android.graphics.drawable.Drawable;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.tools.OverlayItems;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class GoogleMapsActivity extends MapActivity implements LocationListener
{
	LocationManager locationManager; //<2>
	  Geocoder geocoder; //<3>
	  MapController mapController; 
	  
	@Override
    public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.google_maps);
		
		Bundle bundle = getIntent().getExtras();
		
		if(bundle != null)
		{
			MapView mapView = (MapView) findViewById(R.id.mapview);
	        mapView.setBuiltInZoomControls(true);
	        MapController mc = mapView.getController();
	        List<Overlay> mapOverlays = mapView.getOverlays();
	        Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
	        OverlayItems overlayItems = new OverlayItems(drawable);
	        
	        GeoPoint geoPoint = new GeoPoint(bundle.getInt(CourseController.LATITUDE), bundle.getInt(CourseController.LONGITUDE));
	        
	        overlayItems.addOverlay(new OverlayItem(geoPoint, "", ""));
	        mapOverlays.add(overlayItems);
	        mc.setZoom(16);
	        
	        locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE); //<2>
	        
	        geocoder = new Geocoder(this);
	        
	        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //<5>
	        Log.i("location", location + "");
	        if (location != null) {
	          this.onLocationChanged(location);
	        }
		}
	}
	
	@Override
	protected boolean isRouteDisplayed()
	{
		return false;
	}
	
	public void onLocationChanged(Location location)
	{
	    try {
	      int latitude = (int)(location.getLatitude() * 1000000);
	      int longitude = (int)(location.getLongitude() * 1000000);

	      GeoPoint point = new GeoPoint(latitude,longitude);
	      mapController.animateTo(point); //<11>
	      
	    } catch (Exception e) {
	      Log.e("LocateMe", "Could not get Geocoder data", e);
	    }
	  }

	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}
}
